const { defineConfig } = require('@vue/cli-service')

const webpack = require('webpack');
const path = require('path');
const CopyWebPackPlugin = require('copy-webpack-plugin');

function resolve(targetDir) {
  return path.join(__dirname, './', targetDir);
}

// CesiumJS resources location
const cesiumSource = 'node_modules/cesium/Build/Cesium';
const cesiumSourceWidgets = 'node_modules/cesium/Source/Widgets/widgets.css';
// Cesium.js location for set path alias
// const cesiumSourcePath = 'node_modules/cesium/Source';
// Base URL for static files that CesiumJS needs to load
// Base URL is not required
const cesiumBaseUrl = 'cesium';

module.exports = defineConfig({
  transpileDependencies: true,
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  
  devServer: {
    port: process.env.VUE_APP_PORT,
    proxy: {
      '/api': {
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/'
        }
      }
    }
  },

  configureWebpack: {
    resolve: {
      fallback: { https: false, zlib: false, http: false, url: false },
      mainFiles: ["index", "Cesium"],
    },
    plugins: [
      // global import jquery
      new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        "windows.jQuery": 'jquery'
      }),
      // Copy Cesium Assets, Widgets, and Workers to a static directory
      new CopyWebPackPlugin({
        patterns: [
          { from: path.join(cesiumSource, 'Assets'), to: `${cesiumBaseUrl}/Assets`},
          { from: path.join(cesiumSource, 'ThirdParty'), to: `${cesiumBaseUrl}/ThirdParty`},
          { from: path.join(cesiumSource, 'Widgets'), to: `${cesiumBaseUrl}/Widgets`},
          { from: path.join(cesiumSource, 'Workers'), to: `${cesiumBaseUrl}/Workers`},
        ],
      }),
      // Define relative base path in cesium for loading assets
      new webpack.DefinePlugin({
        CESIUM_BASE_URL: JSON.stringify('./' + cesiumBaseUrl),
      })
    ],
  },
  chainWebpack: (config) => {
    // set alias for Cesium.js
    // config.resolve.alias.set('cesium', resolve(cesiumSourcePath));
    config.resolve.alias.set('widgets.css', resolve(cesiumSourceWidgets));
  },
})
